package com.core.convenientCode;

import com.common.ErrorCodeEnum;
import com.common.exception.BusinessException;
import com.common.utils.StringUtils;
import com.core.convenientCode.common.GenUtils;
import com.dao.primary.sys.SysGenTableColumnMapper;
import com.model.pojo.sys.SysGenTable;
import com.model.pojo.sys.SysGenTableColumn;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zql
 * @Description: 表字段管理表
 * @Data: 10:29 2021/6/1
 */
@Service
public class SysGenTableColumnService {
    @Resource
    SysGenTableColumnMapper sysGenTableColumnMapper;

    @Resource
    SysGenTableService sysGenTableService;

    /**
     * @Author: zql
     * @Description: 从information_schema.columns表中获取字段信息
     * @param tableName
     * @Data: 10:49 2021/6/1
     */
    public List<SysGenTableColumn> getTableColumnListForDB(String tableName){
        return sysGenTableColumnMapper.selectDBList(tableName);
    }

    public List<SysGenTableColumn> getTableColumnList(Long sysGenTableId){
        return sysGenTableColumnMapper.selectBSysGenTableId(sysGenTableId);
    }

    public void deleteByTableId(Long sysGenTableId){
        sysGenTableColumnMapper.deleteBySysGenTableId(sysGenTableId);
    }

    /**
     * @Author: zql
     * @Description: 同步表字段
     * @param tableName
     * @Data: 15:26 2021/6/1
     */
    public void syncTableColumn(String tableName) {

        SysGenTable sysGenTable = sysGenTableService.getSysGenTable(tableName);
        if(StringUtils.isNull(sysGenTable)){
            throw new BusinessException(ErrorCodeEnum.SYNC_TABLE_COLUMN_FAILED);
        }
        //从表中获取字段
        List<SysGenTableColumn> sysGenTableColumnList = getTableColumnList(sysGenTable.getSysGenTableId());
        List<String> tableColumnNames = sysGenTableColumnList.stream().map(SysGenTableColumn::getColumnName).collect(Collectors.toList());

        //从DB中获取字段
        List<SysGenTableColumn> sysGenTableColumnListDB = getTableColumnListForDB(sysGenTable.getTableName());
        if (StringUtils.isEmpty(sysGenTableColumnListDB)) {
            throw new BusinessException(ErrorCodeEnum.SYNC_TABLE_COLUMN_FAILED);
        }
        List<String> tableColumnNamesDb = sysGenTableColumnListDB.stream().map(SysGenTableColumn::getColumnName).collect(Collectors.toList());

        //如果表中字段不存在就增加新的
        sysGenTableColumnListDB.forEach(column -> {
            GenUtils.initColumnField(column, sysGenTable);
            if (!tableColumnNames.contains(column.getColumnName())) {
                //不存在就插入新的
                sysGenTableColumnMapper.insertSelective(column);
            } else {
                //存在就更新
                sysGenTableColumnMapper.updateByColumnName(column);
            }
        });

        //获取哪些现在数据库表中不存在的字段
        List<SysGenTableColumn> sysGenTableColumnListNotFound = sysGenTableColumnList.stream().
                filter(column -> !tableColumnNamesDb.contains(column.getColumnName())).collect(Collectors.toList());

        //删除
        if (StringUtils.isNotEmpty(sysGenTableColumnListNotFound)) {
            for (SysGenTableColumn sysGenTableColumn : sysGenTableColumnListNotFound) {
                sysGenTableColumnMapper.deleteByPrimaryKey(sysGenTableColumn.getSysGenTableColumnId());
            }
        }
    }


}
